home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / CGI / C_DOWNLOAD.CGI-S=TEXTCOUNTER&C=TXT&F=README < prev    next >
Text File  |  1996-06-03  |  17KB  |  314 lines

  1. /*****************************************************************************
  2. * TextCounter (C++ Version)     Version 1.3                                  *
  3. * Copyright 1996 Matt Wright    mattw@worldwidemart.com                      *
  4. * Created 03/14/96              Last Modified 03/28/97                       *
  5. * Matt's Script Archive, Inc.   http://www.worldwidemart.com/scripts/        *
  6. * Perl Version also available at Matt's Script Archive.                      *
  7. ******************************************************************************
  8. * If you run into any problems while trying to configure this program, help  *
  9. * is available.  The steps you should take to get the fastest results, are:  *
  10. *       1) Read this file thoroughly                                         *
  11. *       2) Consult the Matt's Script Archive Frequently Asked Questions:     *
  12. *               http://www.worldwidemart.com/scripts/faq/                    *
  13. *       3) If you are still having difficulty installing this program, send  *
  14. *          e-mail to: scripts-help@tahoenet.com                              *
  15. *          Include any error messages you are receiving and as much detail   *
  16. *          as you can so we can spot your problem.  Also include the variable*
  17. *          configuration block that is located at the top of the program.    *
  18. *                                                                            *
  19. * Hopefully we will be able to help you solve your problems.  Thank you.     *
  20. ******************************************************************************
  21. * COPYRIGHT NOTICE                                                           *
  22. * Copyright 1996 Matthew M. Wright  All Rights Reserved.                     *
  23. *                                                                            *
  24. * TextCounter may be used and modified free of charge by anyone so long as   *
  25. * this copyright notice and the comments above remain intact.  By using this *
  26. * code you agree to indemnify Matthew M. Wright from any liability that      *
  27. * might arise from it's use.                                                 *
  28. *                                                                            *
  29. * Selling the code for this program without prior written consent is         *
  30. * expressly forbidden.  In other words, please ask first before you try and  *
  31. * make money off of my program.                                              *
  32. *                                                                            *
  33. * Obtain permission before redistributing this software over the Internet or *
  34. * in any other medium.  In all cases copyright and header must remain intact.*
  35. *****************************************************************************/
  36.  
  37. TextCounter 1.3 is a simple program which allows you to include a text counter
  38. on any web page.  You can also include the date since it began counting, a 
  39. link to a help page, digit padding, and more.  This program was designed for 
  40. anyone to use, from a user who just wants a text counter on his or her home
  41. page to a system administrator who wants to make it easy for anyone on their
  42. server to use the counter program.  Multiple counters can be set up, with the
  43. need for only one program to manage them all.  You can specify what
  44. directories are allowed to access this program, and can even allow certain
  45. directories or exclude certain directories from being able to use this script.
  46. Details on how to install and use this script are available below.
  47.  
  48. Version 1.3 now allows a QUERY_STRING to be appended to the CGI program call,
  49. in which case that name will be used as the file to store to rather than the
  50. DOCUMENT_URI.  This allows the textcounter to work on servers which don't
  51. support the DOCUMENT_URI environment variable.  I have heard some Netscape
  52. servers to not support this, and thus you may need to use the QUERY_STRING.
  53.  
  54. A Perl version of this counter is also available at Matt's Script Archive,
  55. however for frequently used programs such as a counter, C++ definitely has
  56. an advantage over Perl.  I ran a few tests (nothing extremely standard) and
  57. found that the C++ version runs about 5 times faster, at about .03 seconds,
  58. compared to the Perl version which runs in about .15 seconds.
  59.  
  60. This package should have come with 2 files:
  61.       1) README       - This file.  Installation Instructions, Disclaimer, 
  62.                         Copyright, etc...
  63.       2) tcounter.cpp - The C++ CGI program which does all of the work.
  64.  
  65. Installation:
  66. =============
  67.  
  68. tcounter.cpp
  69. ------------
  70.  
  71. There are several Variables and Options you will need to configure.  
  72. The instructions below provide examples and instructions of how to do so.
  73.  
  74. VARIABLE CONFIGURATION:
  75.       const char data_dir[] = "/path/to/data/";
  76.             The data_dir variable should specify the path to the 
  77.             directory under which all of the data files will be stored.
  78.             This path must end with a '/' and it NEEDS to be writable by 
  79.             your web server.  This means that you most likely will need 
  80.             to chmod this directory 666.  You can do this by executing:
  81.  
  82.                   chmod 666 /path/to/data/
  83.  
  84.             It is suggested that you make a new directory for the sole
  85.             purpose of holding the data files.  A new data file will be
  86.             created for each page you add your text counter to.  You 
  87.             may think that this is not the best way to do this, but it
  88.             is not all bad, and is beneficial in some ways:
  89.  
  90.                   1) If you use this system wide, it is likely that 
  91.                      many pages will load at the same time, meaning 
  92.                      this program would have to try and edit the main 
  93.                      file if it was all included in one database.  
  94.                      This file can lead to slow downs (if I locked the
  95.                      file each time it was called) or it could lead to 
  96.                      mangled data if I didn't.  That is one reason I 
  97.                      chose to use separate files.
  98.                   2) The files created for the data are EXTREMELY 
  99.                      small, taking up between 15 and 30 bytes (yes, 
  100.                      you heard correctly, bytes).
  101.                   3) Access time is faster as I know exactly what 
  102.                      file to open, rather than flipping through lines 
  103.                      of a database if it was all in one file.
  104.                   (BTW, this explanation was more for people who 
  105.                   may have questioned why I chose to do it this way.  
  106.                   Most of you could care less about these last 3 points)
  107.                   :-)
  108.  
  109.       const int  num_valid_uri = 0;
  110.       const char valid_uri[num_valid_uri][128] = { };
  111.             The valid_uri array allows you to allow this program to 
  112.             be used only under a certain directory of your server.  Say 
  113.             your username is fred and you are on a server called 
  114.             host.com, therefore all of your pages reside under: 
  115.             http://www.host.com/~fred/.  You only want those pages under
  116.             that directory to be able to use this program, so you set the 
  117.             valid_uri array to { "/~fred" }; and the num_valid_uri to the
  118.             number of directories you allow, or 1 in this case. To let 
  119.             your friend joe use it, by set the valid_uri variable to:
  120.             { "/~fred", "/~joe" }; and num_valid_uri to 2.  Or if you are
  121.             a sysadmin who wants to allow everyone to use this program, 
  122.             simply set this array to { "/" }; and num_valid_uri to 0 along
  123.             with the num_invalid_uri to 0.  If your web server supports
  124.             the environment variable DOCUMENT_URI, you will most likely
  125.             wish to set this variable to:
  126.                   const int  num_valid_uri = 1;
  127.                   const char valid_uri[num_valid_uri][128] = { "/" };
  128.  
  129.       const int  num_invalid_uri = 0;
  130.       const char invalid_uri[num_invalid_uri][128] = { };
  131.             Most likely you will just comment this line out if you do 
  132.             not wish to block access to a certain part of your server.
  133.             But take the example of fred above.  He decides to be real 
  134.             cool and open this program up to anyone on the server by 
  135.             setting valid_uri to { "/" };  His arch enemy bob is also on 
  136.             the server though, and fred despises him so much that he 
  137.             wants to block access to this guy, cause bob is such a jerk.
  138.             So fred sets his invalid_uri to { "/~bob" }; so bob can't use 
  139.             his counter. MU HA HA HA.  I'm sure there are other cool uses
  140.             for this too.  Like if you sell virtual domains and want to 
  141.             charge people before they can use your counter program, you 
  142.             put their URI in here until they pay or something.  I dunno. 
  143.             The story was fun to write, and that's all that matters. :-)
  144.  
  145. OPTION SETTINGS:
  146.       const char show_link[] = "http://www.worldwidemart.com/scripts/";
  147.             If you put a URL into this option, then the actual number 
  148.             returned by the TextCounter program will be linked to this 
  149.             URL.  This is useful if you want to link to my site 
  150.             (PLEASE?!) or link to a help page explaining how the user 
  151.             on your system can set up their own text counter.  Or if 
  152.             you just want to have a pointless link on your number.  
  153.             Setting this to a null value of "" will take out the
  154.             link.
  155.  
  156.       const int auto_create = 1;
  157.             Suggested value here is 1, or else you will have to 
  158.             create data files by hand.  This allows users who reside 
  159.             under the valid_uri array to create a new counter for their 
  160.             page simply by putting the Server Side Include reference into
  161.             their page.  Otherwise the maintainer will have to create a 
  162.             data file which looks like:
  163.  
  164.                   0 January 1, 2000
  165.  
  166.             Obviously putting the correct date into the program and 
  167.             changing 0 to whatever you want to start the number at.  
  168.             This file MUST be writable by the web server meaning you 
  169.             need to chmod it 666.  This means other users on your 
  170.             system can write to it too, which is another reason to allow 
  171.             auto-create.  Auto-Create will leave it chmoded so only the 
  172.             web server can write to it. (usually)
  173.  
  174.       const int show_date = 1;
  175.             If this variable is on, then the date on which you began 
  176.             the count will appear with your actual count number.  It 
  177.             will look like: 
  178.  
  179.                   [Count] hits since [Date]
  180.  
  181.             If this is turned off, you allow users more control over 
  182.             their text and it will simply print:
  183.  
  184.                   [Count]
  185.  
  186.             The user can then supply the date if they wish.
  187.  
  188.       const int lock_sec = 2;
  189.             The lock_sec variable defines how long the program will
  190.             wait for the lock file to be cleared out, before
  191.             overwriting the current lock file.  Often times, the
  192.             count file would get overwritten in the older versions
  193.             because there were no locks on the files, and when two
  194.             users accessed at once, it messed things up.
  195.  
  196.             There are now built in lock routines, but if a user stops
  197.             the process or your machine gets turned off or re-booted 
  198.             while the lock file is still in the directory, that lock 
  199.             file needs to get removed somehow.  The lock_sec variable
  200.             tells the program how long it should wait before deciding
  201.             that the lock file is not valid.
  202.  
  203.             Most of the time the program should not take longer than .1
  204.             seconds to execute, but to be safe I set the default to 
  205.             about 2 seconds in case you're on a REALLY slow server.
  206.             You can vary this depending on whether you think your
  207.             system will operate much faster or slower, but it must be
  208.             a whole number, and I wouldn't go below 1.
  209.  
  210.       const int pad_size = 5;
  211.             You will notice if you have seen many other counters on 
  212.             the web, graphical or text-based, that they are often 
  213.             padded with zeros at the front to form a number like: 
  214.             0000154.  This is achieved by adding 0's to the front of 
  215.             the current count.  In Version 1.3, you can specify how many
  216.             digits long you want your number to be, so in the example 
  217.             above you would set pad_size to 7;
  218.  
  219.             If you do not want your number padded and wish for the 
  220.             above example to appear as 154 in your page, then set 
  221.             pad_size = 1;
  222.  
  223. ______________________________________________________________________________
  224.  
  225. COMPILING THIS PROGRAM
  226.  
  227. This program was written to compile on a BSDI BSD/OS version 2.1 with the
  228. g++ compiler.  In order to compile it and get it ready for execution:
  229.  
  230. g++ tcounter.cpp
  231. mv a.out tcounter
  232.  
  233. You can change tcounter above to tcounter.cgi if you need .cgi extensions for
  234. CGI programs to work on your web server.
  235.  
  236. I tried to compile under MS Visual C++ Version 4.0 and received several
  237. errros.  If you wish trun it under Windows and use MS Visual C++ to compile,
  238. you will need to remove reference to unistd.h at the top and remove the
  239. check_lock and cleanup subfunctions.  I also received errors of allocating
  240. arrays of constant size 1, so you may need to find a work around for that.
  241. I have no idea how it compiles under other OS's, however I would like to know
  242. if it works or not for you.
  243.  
  244. If the compiling and stuff doesn't work out, an easier way of getting a
  245. TextCounter for your web page may be to go to Matt's Script Archive and
  246. download the Perl version.
  247. ______________________________________________________________________________
  248.  
  249. HOW TO CALL THIS PROGRAM FROM YOUR PAGE
  250.  
  251. Calling this program is really very simple.  As I have mentioned before, 
  252. you will need Server Side Includes turned on on your server before you 
  253. can use this program.  Talk to your system administrator or visit my 
  254. Frequently Asked Questions section for more information on server side 
  255. includes.  If you know they are turned on, or want to try and find out, 
  256. put the following code into your HTML document:
  257.  
  258. <!--#exec cgi="/url/path/to/tcounter"-->
  259.  
  260. OR
  261.  
  262. <!--#include virtual="/url/path/to/tcounter"-->
  263.  
  264. So, if I have my tcounter (or tcounter.cgi if I have to rename it for 
  265. my server) program located at 
  266. http://www.worldwidemart.com/scripts/demos/textcounter/tcounter, then I 
  267. would put the following into any HTML document a wanted a count to appear in:
  268.  
  269. <!--#exec cgi="/scripts/demos/textcounter/tcounter"-->
  270.  
  271. OR
  272.  
  273. <!--#include virtual="/scripts/demos/textcounter/tcounter"-->
  274.  
  275. Version 1.3 also allows the program to be called with a QUERY_STRING, unless
  276. valid_uri has been set.  If valid_uri has been set, your system supports 
  277. DOCUMENT_URI anyway, and this is not needed.  However, for servers that don't
  278. support the DOCUMENT_URI environment variable, you can call your program as:
  279.  
  280. <!--#include virtual="/scripts/demos/textcounter/tcounter?unique_id"-->
  281.  
  282. Where unique_id is a string of characters that will be used as the filename
  283. for your count.  The QUERY_STRING option could also be used to keep a count
  284. of your entire site.  For instance, if you put the following on all your
  285. pages:
  286.  
  287. <!--#include virtual="/scripts/demos/textcounter/tcounter?unique_id"-->
  288.  
  289. Where unique_id stays the same on each page you place the counter on, that
  290. same count file woudl be used to count all your web site's web pages and
  291. you could have a counter for your entire web site.
  292. _____________________________________________________________________________
  293.  
  294. Well, that covers all of the options and variables, and I explained these 
  295. in such detail that hopefully I won't get swamped by help mail for this 
  296. program. :-D (Yah right!)  But please, before you ask for help, follow the 
  297. steps at the top of this document.  You probably won't get an answer from 
  298. myself or the help staff if your answer is in the Frequently Asked 
  299. Questions at my site.
  300. _____________________________________________________________________________
  301.  
  302. HISTORY:
  303. Version 1.0  - 03/14/96 -      * TextCounter Created and Released.
  304. Version 1.1  - 04/25/96 -      * @valid_referer array and checking 
  305.                                  removed.  Because server side includes can 
  306.                                  only be used locally, it is unnecessary.  
  307.                                  Also, it was causing many counters to 
  308.                                  incorrectly display error messages.
  309. Version 1.2  - 05/10/96 -      * File Locking Procedure added.
  310.                                * Options lock_sec and pad_size added.
  311. Version 1.3  - 03/29/97 -      * Perl version converted to this C++ version.
  312.                                * QUERY_STRING support added.
  313. _____________________________________________________________________________
  314. Matt Wright - mattw@worldwidemart.com - http://www.worldwidemart.com/scripts/